/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PatientAdministrationBeans;

import EntityClass.Bed;
import EntityClass.Patient;
import EntityClass.mCase;
import Exception.ExistException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Ashish
 */
@Stateless
public class AdministrativeAdmissionBean{

    @PersistenceContext
    EntityManager em;
    private Patient patient;
    //private mCase mcase;

    /** Creates a new instance of CustomerManagerBean */
    public AdministrativeAdmissionBean() {}

    
    public void addPatient(String NRIC, String name, Date birthday, String address, String cNumber) {
        //throws ExistException, Exception{
        // if(customerEntity != null) // Customer exists
        //throw new ExistException ("CUSTOMER ALREADY EXISTS");
        patient = new Patient();
        patient.create(NRIC, name, birthday, address, cNumber);
        //contactEntity = createContact(address);
        //customerEntity.setContact(contactEntity);
        em.persist(patient);
    }
    
    public void makeAppointment() {
        
    }
    
   @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void createCase (Long bedNo) {
        //throws ExistException {
        mCase mcase = new mCase();
        //DateFormat dateCreated = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date dateCreated = new Date();
        mcase.create(dateCreated);
               
        Bed bed = em.find(Bed.class, bedNo);
        if(bed == null) { // Bed does not exist
            em.clear();
            //throw new ExistException("BED DOES NOT EXIST");
        }
        mcase.setBed(bed);
        bed.setmCase(mcase);
        em.persist(mcase);
        //em.persist(bed);
        em.flush();
    }
   
   //Get available beds
   public List<Bed> getBeds() {
        Query q = em.createQuery("SELECT b FROM Bed b");
        List stateList = new ArrayList();
        for (Object o: q.getResultList()) {
            Bed b = (Bed)o;
            if(b.getmCase()== null)
            stateList.add(b);
        }
        return stateList;
    }
   
    public void CheckBedandWard() throws Exception {  
     }
    
    public void AssignBedandWard() throws Exception {  
     }
    
    public void UpdatePatientInfo() throws Exception {           
     }

}
